package com.handtransaction.controller;

import com.handtransaction.entity.User;
import com.handtransaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * @author YAO
 * @create 2021-03-31 16:58
 */
/**
 * 处理图片文件上传的控制器
 */

/**
 * 只是拿来做图片上传回显的测试
 */
@Controller
public class fileUploadController {

	@Autowired
	UserService userService;

    @RequestMapping(value = "fileupload",method = RequestMethod.POST)
    public String Photoupload(Model model,
							  @RequestParam(value = "uname",required = false)String username,
							  @RequestParam(value = "Iimg",required = false) MultipartFile file,
							  HttpServletRequest request, HttpServletResponse response,User user)throws IOException {
		//保存数据库的路径
		String sqlPath = null;
		//定义文件保存的本地路径
		String localPath="D:\\SecondHandPhotos\\";
		//定义 文件名
		String filename=file.getOriginalFilename();
		//文件保存路径
		file.transferTo(new File(localPath+filename));
		//把图片的相对路径保存至数据库
		sqlPath = "/images/"+filename;
		System.out.println(sqlPath);
		user.setUimg_path(sqlPath);
//		userService.UpdateUserInfo(user);
		model.addAttribute("user", user);

		 return "common/testuserimg";

	}

}
